package com.xhj.bigfactory.bigFactory.Lock;

import java.util.concurrent.CyclicBarrier;

import static java.lang.System.out;
import static java.lang.Thread.currentThread;

/**
 * -
 *
 * @author 周沫 @Date 2021-07-30 @Time 0:12
 */
public class CyclicBarrierDemo {

  public static void main(String[] args) {
    CyclicBarrier cyclicBarrier =
        new CyclicBarrier(
            7,
            () -> {
              out.println("\n召唤神龙");
            });
    for (int i = 0; i < 9; i++) {
      int finalI = i;
      new Thread(
              () -> {
                out.println(currentThread().getName() + ": 已经集齐第" + (finalI + 1) + "颗龙珠");
                try {
                  cyclicBarrier.await();
                } catch (Exception e) {
                  e.printStackTrace();
                } finally {
                }
              },
              "t" + i)
          .start();
    }
    new Thread(
            () -> {
              out.println(currentThread().getName() + " come in...");
            },
            "")
        .start();
  }
}
